package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.pojo.provider;
import cn.smbms.service.BillService;
import cn.smbms.service.providerService;
import cn.smbms.util.Constants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname ProviderController
 * @Description TODO
 * @Date 2020-10-11 11:51
 * @Created by 26440
 */
@Controller
public class ProviderController {

    @Autowired
    private providerService providerService;

    @Autowired
    private BillService billService;

    @RequestMapping("provider.do")
    public String providerList(HttpServletRequest request,
                               String queryProCode,
                               String queryProName,
                               @RequestParam(defaultValue = "1") Integer pageIndex) {

        QueryWrapper<provider> wrapper = new QueryWrapper<provider>();
        wrapper.like(!StringUtils.isEmpty(queryProCode), "proCode", queryProCode);
        wrapper.like(!StringUtils.isEmpty(queryProName), "proName", queryProName);
        PageHelper.startPage(pageIndex, 8);
        List<provider> providerList = providerService.list(wrapper);

        PageInfo pi = new PageInfo(providerList);
        request.setAttribute("totalPageCount", pi.getPages());
        request.setAttribute("totalCount", pi.getTotal());
        request.setAttribute("currentPageNo", pi.getPageNum());

        request.setAttribute("providerList", providerList);
        return "providerlist";
    }

    /**
     * 查看供应商信息
     *
     * @return
     */
    @RequestMapping("providerView.do/{id}")
    public String providerView(@PathVariable("id") Integer proid, HttpServletRequest request) {
        provider provider = providerService.list(new QueryWrapper<provider>().eq("id", proid)).get(0);
        request.setAttribute("provider", provider);
        return "providerview";
    }

    /**
     * 添加功能先查询
     *
     * @param request
     * @return
     */
    @RequestMapping("providerAdd.do")
    public String providerAdd(HttpServletRequest request) {

        return "provideradd";
    }

    @RequestMapping("providerSave.do")
    public String providerSave(@Valid provider provider, HttpServletRequest request) {
        //获取当前用户
        User logUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        //获取id
        logUser.setCreatedBy(logUser.getId());
        //获取当前日期
        logUser.setCreationDate(new Date());
        provider.setCreationDate(new Date());
        if (providerService.save(provider)) {
            //添加成功
            return "redirect:provider.do";
        }
        return "redirect:provideradd.jsp";
    }

    /**
     * 修改(前绑定数据)
     *
     * @param proid
     * @param request
     * @return
     */
    @RequestMapping("providerModify.do")
    public String providerModify(Integer proid, HttpServletRequest request) {
        provider provider = providerService.getById(proid);
        request.setAttribute("provider", provider);
        return "providermodify";
    }

    @RequestMapping("providerUpdate.do")
    public String providerUpdate(provider provider, HttpServletRequest request) {
        User LogUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        provider.setModifyBy(LogUser.getId());
        provider.setModifyDate(new Date());
        if (providerService.updateById(provider)) {
            //成功
            return "redirect:provider.do";
        }
        return "redirect:providerModify.do?proid=" + provider.getId();
    }

    /**
     * 删除供应商信息
     * @param proid
     * @return
     */
    @RequestMapping("providerDelete.do")
    @ResponseBody
    public Object providerDelete(Integer proid){
        Map<String, Object> map = new HashMap<String, Object>();

        if (providerService.removeById(proid)) {
            map.put("delResult", "true");
        } else {
            map.put("delResult", "false");
        }
        return map;
    }

}
